package com.bdc.utils;

import com.sun.xml.internal.messaging.saaj.packaging.mime.internet.MimeUtility;

import java.io.ByteArrayOutputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;

/**
 * User: Shawn
 * Date: 2011-6-30
 * Time: 13:58:56
 */
public class StringUtils {

    public static String fromUtf8(String string) throws UnsupportedEncodingException {
        if (string == null) return null;
        return new String(string.getBytes("ISO-8859-1"));
    }

    public static String toUtf8(String string) throws UnsupportedEncodingException {
        if (string == null) return null;
        return new String(string.getBytes(), "ISO-8859-1");
    }

    public static String SHAString(String pass) throws Exception {
        MessageDigest md = MessageDigest.getInstance("SHA");
        byte[] digest = md.digest(pass.getBytes("iso-8859-1"));
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        OutputStream encodedStream = MimeUtility.encode(bos, "base64");
        encodedStream.write(digest);
        return bos.toString("iso-8859-1");
    }
}
